/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package onpu_dms_xls.API;

import java.io.File;
import java.sql.Connection;
import java.sql.SQLException;

import onpu_dms_xls.exceptions.MyException;
import onpu_dms_xls.exceptions.MySQLException;
import onpu_dms_xls.kernel.File_From_DB;
import onpu_dms_xls.kernel.TemplatesLoader;
import onpu_dms_xls.kernel.XLS_2_DB;
import onpu_dms_xls.kernel.common.logAndDebug.LoggingFacade;
import org.apache.log4j.PropertyConfigurator;

/**
 * One class for the document management systemm of ONPU to access functionality
 * of the program.
 * @author snbm
 */
public class API {
    private static API obj;

    private API() {}

    public static API getInstance() {
        if (obj == null) {
            obj = new API();
        }
        return obj;
    }

    public static void XLS2DB(String xml, Connection conn, File inFile) throws MyException {
        long startTime = System.currentTimeMillis();
        try {
            (new XLS_2_DB()).read_XLS_And_Process_It_To_DB(xml, conn, inFile);
        } catch (MyException e) {
            throw e;
        } catch (Exception e) {
            LoggingFacade.trace.fatal("unexpected error (API, XLS2DB, calling the read_XLS_And_Process_It_To_DB):", e);
            return;
        }
        LoggingFacade.performance.info("time:" + (System.currentTimeMillis() - startTime));
    }

    public File XLSFromDB(
            String xml, Connection con)
            throws MyException {
        long startTime = System.currentTimeMillis();
        LoggingFacade.trace.info("call, inputXML=" + xml);
        File res;
        File_From_DB ffdb;
        try {
            ffdb = File_From_DB.get_XLS_File_From_DB_Processor(xml, con);
            res = ffdb.get_File_From_DB();
        } catch (SQLException e) {
            throw new MySQLException("... caught in API", "get_file :-)", "", "", e.getMessage());
        }
        LoggingFacade.performance.info("time:" + (System.currentTimeMillis() - startTime));
        return res;
    }

    public File pdfFromDB(
            String xml, Connection con)
            throws MyException{
        long startTime = System.currentTimeMillis();
        LoggingFacade.trace.info("call, inputXML=" + xml);
        File res;
        File_From_DB ffdb;
        try {
            ffdb = File_From_DB.get_PDF_File_From_DB_Processor(xml, con);
            res = ffdb.get_File_From_DB();
        } catch (SQLException e) {
            throw new MySQLException("... caught in API", "get_file :-)", "", "", e.getMessage());
        }
        LoggingFacade.performance.info("time:" + (System.currentTimeMillis() - startTime));
        return res;
    }

    //debugMode value is ignored
    @Deprecated
    public void loadTemplates(
            Connection con,
            File xlsTemplate,
            File xmlTemplate,
            int debugMode,
            int documentClassId,
            int userId) throws MyException {
        long startTime = System.currentTimeMillis();
        LoggingFacade.trace.info("call, params: xlsTemplate=" + xlsTemplate.getAbsolutePath()
                + ", xmlTemplate=" + xmlTemplate.getAbsolutePath()
                + ", debugMode=" + debugMode
                + ", documentClassId=" + documentClassId
                + ", userId=" + userId);
        TemplatesLoader tl = TemplatesLoader.getTemplatesLoader(TemplatesLoader.TemplatesLoaders.DB);
        tl.loadTemplates(con, xlsTemplate, xmlTemplate, documentClassId, userId);
        LoggingFacade.performance.info("time:" + (System.currentTimeMillis() - startTime));
    }

    public void loadTemplates(
            Connection con,
            File xlsTemplate,
            File xmlTemplate,
            int documentClassId,
            int userId) throws MyException {
        long startTime = System.currentTimeMillis();
        LoggingFacade.trace.info("call, params: xlsTemplate=" + xlsTemplate.getAbsolutePath()
                + ", xmlTemplate=" + xmlTemplate.getAbsolutePath()
                + ", documentClassId=" + documentClassId
                + ", userId=" + userId);
        TemplatesLoader tl = TemplatesLoader.getTemplatesLoader(TemplatesLoader.TemplatesLoaders.DB);
        tl.loadTemplates(con, xlsTemplate, xmlTemplate, documentClassId, userId);
        LoggingFacade.performance.info("time:" + (System.currentTimeMillis() - startTime));
    }


}
